/**
 * 附件区域，是一个span节点
 */
var attachmentSection=element('attachment');


/**
 * 对话框按钮
 * @type 
 */
var submitButton={text:"添加", handler:function(){this.submit()}, isDefault:true};
var cancelButton={text:"取消", handler:function(){this.cancel()}};

/**
 * 附件处理成功后，request、delete、upload后触发
 * @param {} o
 */
var onAttachmentSuccess=function(o){
	//更新promotion数据
	var list=[];
	list = JSON.parse(o.responseText);
	
	promotion.attachmentList=list;
	showAttachment();
};

/*
 * 
 * 
 * 处理附件提交失败
 * @param {} o
 */
var onAttachmentFailure=function(o){
	
};

/**
 * 附件对话框
 */
var attachmentDialog=new Dialog('attachmentDialog', 
								{width:"550px",
								fixedcenter:true,
								modal:true,
								buttons : [submitButton, cancelButton],
								callback: {success:onAttachmentSuccess, upload:onAttachmentSuccess,failure:onAttachmentFailure}
								}
);

/**
 * 用户在对话框中点击"删除"链接,删除指令发送后，服务器将返回新的附件清单
 * @param {} fileName
 */
var onDeleteAttachmentClick=function(fileName) {
	var callback = {
		cache : false,
		success : onAttachmentSuccess
	};
	Connect.asyncRequest('DELETE', '/promotion/' + promotion.id + '/attachment/'+fileName, callback);
	attachmentDialog.hide();
};

/**
 * 用户单击"附件"链接后，显示编辑附件的对话框
 */
var onAttachmentLinkClick=function() {
	attachmentDialog.setHeader('编辑附件');
	
		var html='';
		var list=promotion.attachmentList;
		
		for (var i = 0, len = list.length; i < len; ++i) {
			html=html+list[i].fileName;
			html=html+'&nbsp;&nbsp;&nbsp;';
			html=html+'<a href="#" onclick="javascript:onDeleteAttachmentClick(\''+list[i].fileName+'\')">删除</a>';
			html=html+'<br />'
		};
		html=html+'<form action="/promotion/'+promotion.id+'/upload">';
		html=html+'<input type="file" name="file" size="50"/>';
		html=html+'</form>';
	
	attachmentDialog.setBody(html);
	attachmentDialog.render();
	attachmentDialog.show();
};

/**
 * 向服务器请求文件清单
 */
var requestAttachmentList=function(){
	var callback = {
		cache : false,
		success : onAttachmentSuccess
	};
	Connect.asyncRequest('GET', '/promotion/' + promotion.id + '/attachmentList', callback);
};

/**
 * 注册附件链接点击事件
 */
Event.on('attachmentLink', 'click', onAttachmentLinkClick);

//requestAttachmentList();	//TODO 测试代码
